package com.css.zfzx.sjcj.modules.classifycode.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.utils.PlatformPageUtils;
import com.css.zfzx.sjcj.modules.classifycode.repository.ZfzxClassifyCodeNativeRepository;
import com.css.zfzx.sjcj.modules.classifycode.repository.entity.ZfzxClassifyCodeEntity;
import com.css.zfzx.sjcj.modules.classifycode.repository.impl.ZfzxClassifyCodeNativeRepositoryImpl;
import com.css.zfzx.sjcj.modules.classifycode.viewobjects.ZfzxClassifyCodeQueryParams;
import com.css.zfzx.sjcj.modules.classifygroup.repository.entity.ZfzxClassifyGroupEntity;
import com.css.zfzx.sjcj.modules.classifygroup.repository.impl.ZfzxClassifyGroupNativeRepositoryImpl;
import com.css.zfzx.sjcj.modules.classifygroup.viewobjects.ZfzxClassifyGroupQueryParams;
import com.css.zfzx.sjcj.modules.classifygroup.repository.ZfzxClassifyGroupNativeRepository;
import org.omg.CORBA.RepositoryIdHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;
import org.springframework.beans.factory.support.AbstractBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory;
import org.springframework.beans.factory.support.StaticListableBeanFactory;

/**
 * @author tao
 * @date 2020-11-10
 * 地震灾害对象分类编码类
 */

@ComponentScan
public class SingletonClassifyCode {

    @Autowired
    private ZfzxClassifyCodeNativeRepository zfzxClassifyCodeNativeRepository;
    @Autowired
    private ZfzxClassifyGroupNativeRepository zfzxClassifyGroupNativeRepository;

    private static Map<String,String> classifyCodeMap = new HashMap<>();

    private SingletonClassifyCode() {
    }

    private static class SingletonClassifyCodeHolder {
        private static final SingletonClassifyCode singletonClassifyCode = new SingletonClassifyCode();
    }

    public static SingletonClassifyCode getInstance() {
        return SingletonClassifyCodeHolder.singletonClassifyCode;
    }

    public void setGroupRepository(ZfzxClassifyGroupNativeRepository zcgnr) {
        zfzxClassifyGroupNativeRepository = zcgnr;
    }

    public void setCodeRepository(ZfzxClassifyCodeNativeRepository zccnr) {
        zfzxClassifyCodeNativeRepository = zccnr;
    }

    //classifyName 格式为：大类名称-中类名称-小类名称
    public String getClassifyCode(String classifyName) {
        // 初始化类分类编码
        if(classifyCodeMap.isEmpty()) {
            String jsonRowsKey = "rows";
            // 获取地震灾害分组编码
            ZfzxClassifyGroupQueryParams groupQueryParams = new ZfzxClassifyGroupQueryParams();
            groupQueryParams.setGroupName("地震灾害");
            groupQueryParams.setIsValid("1");
            Page<ZfzxClassifyGroupEntity> zfzxClassifyGroupPage = this.zfzxClassifyGroupNativeRepository.queryZfzxClassifyGroups(groupQueryParams, 1, 1000);
            JSONObject groupJsonObject = PlatformPageUtils.formatPageData(zfzxClassifyGroupPage);
            if(groupJsonObject.isEmpty() || !groupJsonObject.containsKey(jsonRowsKey)){
                return "";
            }
            JSONArray groupJson = groupJsonObject.getJSONArray(jsonRowsKey);
            if(groupJson.isEmpty()) {
                return "";
            }
            JSONObject groupObject = groupJson.getJSONObject(0);
            String groupIdStr = "groupId";
            if(groupObject.isEmpty() || !groupObject.containsKey(groupIdStr))
            {
                return "";
            }
            int groupId = groupObject.getInteger(groupIdStr);

            // 获取具体灾害类编码
            ZfzxClassifyCodeQueryParams codeQueryParams = new ZfzxClassifyCodeQueryParams();
            codeQueryParams.setGroupId(groupId);
            codeQueryParams.setIsValid("1");
            Page<ZfzxClassifyCodeEntity> zfzxClassifyCodePage = this.zfzxClassifyCodeNativeRepository.queryZfzxClassifyCodes(codeQueryParams, 1, 10000);
            JSONObject codeJsonObject = PlatformPageUtils.formatPageData(zfzxClassifyCodePage);
            if(codeJsonObject.isEmpty() || !codeJsonObject.containsKey(jsonRowsKey)){
                return "";
            }
            JSONArray codeJsonArray = codeJsonObject.getJSONArray(jsonRowsKey);
            if(codeJsonArray.isEmpty()){
                return "";
            }

            HashMap<String, List<String>> firstCodes = new HashMap<>();
            HashMap<String,List<String>> secondCodes = new HashMap<>();
            HashMap<String,List<String>> thirdCodes = new HashMap<>();

            //将三类数据分别存放
            for(int i = 0; i < codeJsonArray.size(); ++i) {
                JSONObject value = codeJsonArray.getJSONObject(i);
                if(value.isEmpty()){
                    break;
                }
                String id = value.getString("id");
                String parentId = value.getString("parentId");
                int level = value.getInteger("level");
                String code = value.getString("code");
                String codeName = value.getString("codeName");

                List<String> tempList = new ArrayList<>();
                tempList.add(parentId);
                tempList.add(codeName);
                tempList.add(code);

                switch (level) {
                    case 1: {
                        firstCodes.put(id,tempList);
                        break;
                    }
                    case 2: {
                        secondCodes.put(id,tempList);
                        break;
                    }
                    case 3: {
                        thirdCodes.put(id,tempList);
                        break;
                    }
                    default: {
                        break;
                    }
                }
            }

            //合并中类与小类
            HashMap<String,List<String>> secondNewCodes = new HashMap<>();
            for (HashMap.Entry<String,List<String>> entry : thirdCodes.entrySet()) {
                List<String> thirdValue = entry.getValue();
                List<String> secondValue = secondCodes.get(thirdValue.get(0));
                List<String> newList = new ArrayList<>();
                newList.add(secondValue.get(0));
                newList.add(secondValue.get(1) + "-" + thirdValue.get(1));
                newList.add(secondValue.get(2) + thirdValue.get(2));
                secondNewCodes.put(entry.getKey(),newList);
            }

            //合并大类与中类
            for (HashMap.Entry<String,List<String>> entry : secondNewCodes.entrySet()) {
                List<String> secondValue = entry.getValue();
                List<String> firstValue = firstCodes.get(secondValue.get(0));
                String fullCodeName = firstValue.get(1) + "-" + secondValue.get(1);
                String fullCode = firstValue.get(2) + secondValue.get(2);
                classifyCodeMap.put(fullCodeName,fullCode);
            }
        }

        return classifyCodeMap.get(classifyName);
    }
}
